package com.hhd.flow.controller;

import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.impl.WithdrawCommand;
import com.hhd.flow.vo.Result;
import com.hhd.flow.vo.WithdrawVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow")
public class WithdrawController {

    @Autowired
    FlowEngine flowEngine;

    @PostMapping("/widthdraw")
    public Result widthdraw(@RequestBody WithdrawVo withdrawVo) {
        flowEngine.getFlowActionLogService().logForWithdrawProcess(withdrawVo.getProcessInstanceId(), withdrawVo.getComment());
        WithdrawCommand command = flowEngine.newWithdrawCommandBuilder()
                .comment(withdrawVo.getComment())
                .processInstanceId(withdrawVo.getProcessInstanceId())
                .build();
        flowEngine.execute(command);
        return Result.ok();
    }
}
